Use the golden-section method to find all of the local maxima of the function \[ f(x) = \begin{cases} |x|\log(|x|/2)e^{-|x|} & \text{if } x\neq0\\ 0 & \text{if } x=0 \end{cases} \] over the interval \([-10,10]\).
Plot the function first using curve, which helps you set initial points. Try to use ifelse to define the function.
Using \(f(x,y)=x(x-1)(x+1) - y(y-1)(y+1)\), compare Gradient descent, Line search, and Newton’s method in terms of required steps to reach a local maximum in the region \([-1,0.5]\times[-0.5,1]\). Use \(x_0 = (-.1,.1)\).
X = seq(-1, 1, length=100)
Y = seq(-1, 1, length=100)
XY = expand.grid(X=X, Y=Y) # create a grid
fn <- function(x, y) x*(x-1)*(x+1) - y*(y-1)*(y+1)
Z <- fn(XY$X, XY$Y)
s = interp(x=XY$X, y=XY$Y, z=Z)
plot_ly(x=s$x, y=s$y, z=t(s$z)) %>% add_surface()
In practice, especially for high-dimensional problems, we often have no useful visual information and no idea where to start. In these cases, a common approach is to use random initial points.
Using x0 <- c(runif(1,-.5,3), runif(1,-.5,2)) to generate a random initial point over \([-0.5, 3]\times[-0.5, 2]\), try to find as many local maxima of the following function as possible. \[ f_2(x,y) =\sin\left(\frac{x^2}{2} - \frac{y^2}{4}\right)\cos\left(2x-e^y\right) .\] You should repeat it many times (e.g., 1000 times) and keep a record of local maxima. Which local maxima do you find?